<?php
/**
 * Created by PhpStorm.
 * User: longli
 * VX: isa1589518286
 * Date: 2021/08/23
 * Time: 23:53
 * @link http://www.lmterp.cn
 */

namespace app\common\model;

/**
 * 渠道国家配置
 * Class ChannelCountrySettings
 * @package app\common\model
 */
class ChannelCountrySettings extends BaseModel
{
    protected $pk = 'setting_id';

    protected $autoWriteTimestamp = 'datetime';

    protected $insert = ['create_by', 'update_by'];

    protected $update = ['update_by'];

    /**
     * 关联渠道
     * @return \think\model\relation\BelongsTo
     * @date 2021/08/24
     * @author longli
     */
    public function channel()
    {
        return $this->belongsTo(Channel::class, 'channel_id', 'channel_id');
    }

    /**
     * 关联国家
     * @return \think\model\relation\BelongsTo
     * @date 2021/08/29
     * @author longli
     */
    public function country()
    {
        return $this->belongsTo(SysCountries::class, 'country_code', 'code_two');
    }

    /**
     * 关联报价单
     * @return \think\model\relation\HasMany
     * @date 2021/08/24
     * @author longli
     */
    public function quotation()
    {
        return $this->hasMany(ChannelCountryQuotation::class, 'setting_id', 'setting_id');
    }

    /**
     * 获取物流时效天数
     * @return float
     * @date 2021/08/24
     * @author longli
     */
    public function getTimerSpeed()
    {
        if($this->avg_day > 0) return $this->avg_day;
        $range = explode("-", $this->promise_day);
        return array_sum($range) / count($range);
    }
}
